#include "SaveLoad.h"



std::istream& operator>>(std::istream& f, MYModel*& model){
		//ifstream f;
		//string fi = fichier->getText().createUTF8EncodedString() + string(".txt");
		//f.open(fi);
		if(f.fail()){
			model->setTexte("Echec du chargement!");
			return f;
		}
		int nSize;
		f >> nSize;
		if(nSize != model->getSize()){
			model->setTexte("Incompatible sizes");
			return f;
		}
		model->setSize(nSize);
		model->reload();
		bool tour;
		f >> tour;
		model->setTour(tour);

		bool tourJoue;
		f >> tourJoue;
		model->setTourJoue(tourJoue);

		bool wallSelected;
		f >> wallSelected;
		model->setWallSelect(wallSelected);

		int selectedWall;
		f >> selectedWall;
		model->setSelectedWall(selectedWall);

		double mXR;
		f >> mXR;
		model->setMX(true,mXR);

		double mYR;
		f >> mYR;
		model->setMY(true,mYR);

		double mXB;
		f >> mXB;
		model->setMX(false,mXB);

		double mYB;
		f >> mYB;
		model->setMY(false,mYB);


		Vec4 vColor;
		f >> vColor[0];
		f >> vColor[1];
		f >> vColor[2];
		f >> vColor[3];

		model->setTexte("Loaded!");
		model->setTexteColor(vColor);
		vector<int> p1WallOrientation;
		vector<int> p2WallOrientation;

		for (unsigned i = 0 ; i< model->getp1WallOrientation().size(); i++){
			int orientation;
			f >> orientation;
			model->setp1WallOrientation(i,orientation);
			f >> orientation;
			model->setp2WallOrientation(i,orientation);
			float p1x, p1y, p1z;
			f >> p1x;
			f >> p1y;
			f >> p1z;
			model->setp1WallVec(i,p1x,p1y,p1z);	

			float p2x, p2y, p2z;
			f >> p2x;
			f >> p2y;
			f >> p2z;
			model->setp2WallVec(i,p2x,p2y,p2z);
		}
	return f;
	}

	std::ostream& operator<<(std::ostream& f, MYModel *&model){

		if(f.fail()){
			model->gettexte()->setText("Echec de la sauvegarde!");
			return f;
		}
		f << model->getSize() << " ";
		f << model->tourRouge() << " ";
		f << model->isTourJoue() << " ";
		f << model->isWallSelected() << " ";
		f << model->getSelectedWall() << " ";
		f << model->getMX(true) << " ";
		f << model->getMY(true) << " ";
		f << model->getMX(false) << " ";
		f << model->getMY(false) << " ";
		Vec4 vColor = model->gettexte()->getColor();
		f << vColor[0] << " ";
		f << vColor[1] << " ";
		f << vColor[2] << " ";
		f << vColor[3] << " ";

		for (unsigned i = 0 ; i< model->getp1WallOrientation().size(); i++){
			f << model->getp1WallOrientation()[i] << " ";
			f << model->getp2WallOrientation()[i] << " ";
			f << model->getp1WallVec()[i][0] << " ";
			f << model->getp1WallVec()[i][1] << " ";
			f << model->getp1WallVec()[i][2] << " ";
			f << model->getp2WallVec()[i][0] << " ";
			f << model->getp2WallVec()[i][1] << " ";
			f << model->getp2WallVec()[i][2] << " ";
		}

		model->setTexte("Saved!");
		return f;
	}